package com.yhn.bubblevideo.video.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.bubblevideo.common.enums.BaseErrorCode;
import com.yhn.bubblevideo.common.exception.BusinessException;
import com.yhn.bubblevideo.common.exception.ThrowUtils;
import com.yhn.bubblevideo.model.domain.Comment;
import com.yhn.bubblevideo.model.domain.Video;
import com.yhn.bubblevideo.model.dto.req.common.DeleteRequest;
import com.yhn.bubblevideo.model.dto.req.video.VideoEditRequestDTO;
import com.yhn.bubblevideo.model.dto.req.video.VideoPostRequestDTO;
import com.yhn.bubblevideo.model.dto.req.video.VideoQueryRequestDTO;
import com.yhn.bubblevideo.model.dto.resp.CommentRespDTO;
import com.yhn.bubblevideo.model.dto.resp.VideoRespDTO;
import com.yhn.bubblevideo.video.mapper.VideoMapper;
import com.yhn.bubblevideo.video.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.yhn.bubblevideo.common.constant.CommonConstant.MAX_PAGE_SIZE;

/**
 * @author yhnyhn123
 * @description 针对表【video(视频表)】的数据库操作Service实现
 * @createDate 2024-11-21 13:33:34
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
        implements VideoService {

    /**
     * 分页查询
     */
    @Override
    public Page<VideoRespDTO> queryPage(VideoQueryRequestDTO requestParam) {
        if (requestParam == null) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        long current = requestParam.getCurrent();
        long size = requestParam.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > MAX_PAGE_SIZE, BaseErrorCode.PARAMS_ERROR);
        Page<Video> videoPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(requestParam));
        Page<VideoRespDTO> videoRespPage = new Page<>(current, size, videoPage.getTotal());
        List<VideoRespDTO> videoVO = this.getVideoVO(videoPage.getRecords());
        videoRespPage.setRecords(videoVO);
        return videoRespPage;
    }

    @Override
    public LambdaQueryWrapper<Video> getQueryWrapper(VideoQueryRequestDTO requestParam) {

        if (requestParam == null) {
            return null;
        }

        LambdaQueryWrapper<Video> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper
//                .like(requestParam.getVideoName() != null, Video::get, requestParam.getName());
        return lambdaQueryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param videoList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<VideoRespDTO> getVideoVO(List<Video> videoList) {
        if (CollUtil.isEmpty(videoList)) {
            return new ArrayList<>();
        }
        return videoList.stream().map(this::getVideoVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param video 原对象
     * @return 脱敏对象
     */
    @Override
    public VideoRespDTO getVideoVO(Video video) {
        if (video == null) {
            return null;
        }
        VideoRespDTO videoResp = new VideoRespDTO();
        BeanUtils.copyProperties(video, videoResp);
        return videoResp;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public VideoRespDTO getInfoById(Long id) {
        Video video = baseMapper.selectById(id);
        if (video == null) {
            throw new BusinessException(BaseErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getVideoVO(video);
    }

    /**
     * 生成视频文件
     *
     * @param requestParam 添加请求
     * @return 视频文件ID
     */
    @Transactional
    @Override
    public Long postVideo(VideoPostRequestDTO requestParam) {
        Video video = new Video();
        BeanUtils.copyProperties(requestParam, video);
        baseMapper.insert(video);
        return video.getId();
    }


    /**
     * 修改信息
     *
     * @param requestParam 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateVideo(VideoEditRequestDTO requestParam) {
        if (requestParam == null || requestParam.getId() <= 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        Video video = new Video();
        BeanUtils.copyProperties(requestParam, video);
        long id = requestParam.getId();
        // 判断是否存在
        Video oldVideo = this.getById(id);
        ThrowUtils.throwIf(oldVideo == null, BaseErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(video);
        ThrowUtils.throwIf(!result, BaseErrorCode.OPERATION_ERROR);
        return true;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, BaseErrorCode.NOT_FOUND_ERROR);
        return true;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, BaseErrorCode.NOT_FOUND_ERROR);
        return true;
    }


    /**
     * 获取当前用户发布的视频
     */
    @Override
    public List<VideoRespDTO> getUserVideo(Long userId) {
        LambdaQueryWrapper<Video> queryWrapper = Wrappers.lambdaQuery(Video.class)
                .eq(Video::getUserId, userId);
        List<Video> videos = baseMapper.selectList(queryWrapper);
        return getVideoVO(videos);
    }

    /**
     * 获取视频的所有评论
     */
    @Override
    public List<CommentRespDTO> getVideoComment(Long videoId) {
        // 根据视频id查询对应的评论
        LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery(Comment.class)
                .eq(Comment::getVideoId, videoId);
        return null;
    }


}




